//
//  UrlRequestManager.m
//  LO_MUSIC
//
//  Created by lanou3g on 16/2/29.
//  Copyright © 2016年 杨少锋. All rights reserved.
//

#import "UrlRequestManager.h"

@implementation UrlRequestManager

+(void)requestWith:(requestUrlType)Type URL:(NSString *)URL condition:(NSDictionary *)condition SuccessBlock:(successBlock)successBlock FaileBlock:(faileBlock)faileBlock{
    if (Type == GET) {
        UrlRequestManager *manger = [[UrlRequestManager alloc]init];
        [manger get:URL condition:condition success:^(id item) {
            
        } faile:^(NSError *err) {
            
        }];
    }else{
        UrlRequestManager *manger = [UrlRequestManager new];
        [manger post:URL condition:condition SuccessBlock:^(id item) {
            
        } FaileBlock:^(NSError *err) {
            
        }];
        
    }
    
}

//POST
-(void)post:(NSString *)url condition:(NSDictionary *)condition SuccessBlock:(successBlock)successBlock FaileBlock:(faileBlock)faileBlock{
    

    NSURLSession *session = [NSURLSession sharedSession];
    
    //拼接url
    __block NSMutableString *urlStr = [NSMutableString stringWithString:url];
    //拼接?
    [urlStr appendString:@"?"];
    
    [condition enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        [urlStr appendString:[NSString stringWithFormat:@"%@=%@&",key,obj]];
        
    }];
    
    [urlStr substringToIndex:[urlStr length] - 2];
    //转码
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:urlStr];
    NSString *urlString = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:set];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    //设置请求
    [request setHTTPMethod:@"POST"];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data && !error) {
            id item = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            if (successBlock) {//如果解析成功
                successBlock(item);
                return ;
            }else {
                
                NSLog(@"successBlock为空");
                return;
            }
        }
        
        if (faileBlock) {
            faileBlock(error);
        }
    }];
    
    [task resume];
    
}

//GET

-(void)get:(NSString *)url condition:(NSDictionary *)condition success:(successBlock)success faile:(faileBlock)faile{
        
    NSURLSession *session = [NSURLSession sharedSession];
    
    //拼接url
    NSMutableString *urlStr = [NSMutableString stringWithString:url];
    //拼接?
    [urlStr appendString:@"?"];
    
    NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:urlStr];
    NSString *urlString = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:set];
    
    //body
    __block NSMutableString *bodyStr = [NSMutableString string];
    
    [condition enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
        [bodyStr appendString:[NSString stringWithFormat:@"%@=%@&",key,obj]];
        
    }];
    
    [bodyStr substringToIndex:[bodyStr length] - 2];
    //转码
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    //设置请求
    [request setHTTPMethod:@"GET"];
    
    //设置body
    NSData *date = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:date];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (data && !error) {
            id item = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            if (success) {//如果解析成功
                success(item);
                return ;
            }else {
                
                NSLog(@"successBlock为空");
                return;
            }
        }
        if (faile) {
            faile(error);
        }
    }];
    
    [task resume];
    
}
single_implementation(UrlRequestManager)

@end
